package com.yonyou.crm.sys.quartz.service;

import com.yonyou.crm.base.doc.service.BaseDocDetailBizServiceImpl;
import com.yonyou.crm.common.biz.service.BaseBizServiceImpl;
import com.yonyou.crm.common.field.handler.FieldRelationHandler;
import com.yonyou.crm.common.field.relation.FieldRelation;
import com.yonyou.crm.common.field.relation.RefFieldRelation;
import com.yonyou.crm.common.page.entity.Page;
import com.yonyou.crm.sys.quartz.common.BeanExtendUtil;
import com.yonyou.crm.sys.quartz.dentity.DispatchGroupDO;
import com.yonyou.crm.sys.quartz.dentity.DispatchTaskDO;
import com.yonyou.crm.sys.quartz.ventity.DispatchGroupVO;
import com.yonyou.crm.sys.quartz.ventity.DispatchTaskVO;
import com.yonyou.iuap.context.InvocationInfoProxy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class DispatchGroupBizService extends
		BaseBizServiceImpl<DispatchGroupDO> {
	@SuppressWarnings("unused")
	private final Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private BaseDocDetailBizServiceImpl baseDocDetailBizService;

	/**
	 * 保存任务分组
	 * 
	 * @param vo
	 * @return
	 */
	public DispatchGroupVO saveDispatchGroupVO(DispatchGroupVO vo) {
		// 需要VO转换为DO进行查询
		String s = UUID.randomUUID().toString();// 用来生成数据库的主键id非常不错。。
		vo.setId(s);
		DispatchGroupDO groupDO = this.copyVOToDO(vo);
		Date date = new Date();
		groupDO.setCreated_time(date);
		groupDO.setModified_time(date);
		groupDO.setTs(date);
		byte nn = 0;
		groupDO.setIs_deleted(nn);
		groupDO.setTenantid(InvocationInfoProxy.getTenantid());
		groupDO = super.saveVO(groupDO);
		// 将DO转换为VO
		vo = this.copyDOToVO(groupDO);
		return vo;
	}

	/**
	 * vo转换do
	 * 
	 * @param vo
	 * @return
	 */
	private DispatchGroupDO copyVOToDO(DispatchGroupVO vo) {
		DispatchGroupDO groupDO = new DispatchGroupDO();
		BeanExtendUtil.copyProperties(groupDO, vo);
		return groupDO;
	}

	/**
	 * do转换vo
	 * 
	 * @param groupDO
	 * @return
	 */
	private DispatchGroupVO copyDOToVO(DispatchGroupDO groupDO) {
		DispatchGroupVO vo = new DispatchGroupVO();
		BeanExtendUtil.copyProperties(vo, groupDO);
		return vo;
	}

	/**
	 * 查询任务分组列表
	 * @return
	 */
	public List<Map<String, Object>> selectDispatchGroupList() {
		Map<String, Object> paramMap = new HashMap<>();
		paramMap.put("tenantid", InvocationInfoProxy.getTenantid());
		List<Map<String, Object>> selectMap = dao.selectList(DispatchGroupDO.class, "selectDispatchGroupList", paramMap);
//		List<Map<String, Object>> selectMap = dao
//				.selectMap(
//						"com.yonyou.crm.sm.quartz.dentity.DispatchGroupMapper.selectDispatchGroupList",
//						paramMap);
		return selectMap;
	}
	//分页参照列表
	public Page<DispatchGroupVO> getRefPage(Page<DispatchGroupVO> page, Map<String, Object> paraMap) {
		if (paraMap == null) {
			paraMap = new HashMap<String, Object>();
		}
		paraMap.put("tenantid", InvocationInfoProxy.getTenantid());
		
		List<DispatchGroupVO> vos = page.getData();
		Page<DispatchGroupDO> pageDO = new Page<DispatchGroupDO>();
		List<DispatchGroupDO> taskDOs = new ArrayList<DispatchGroupDO>();
		if (vos != null) {
			for (DispatchGroupVO vo : vos) {
				taskDOs.add(copyVOToDO(vo));
			}
			pageDO.setData(taskDOs);
		}
		pageDO.setOrder(page.getOrder());
		pageDO.setPage(page.getPage());
		pageDO.setPageSize(page.getPageSize());
		pageDO.setSort(page.getSort());
		pageDO.setTotal(page.getTotal());
		if (paraMap != null) {
			pageDO = dao.pageSelect(DispatchGroupDO.class, pageDO, "getRefPage", "countRefPage", paraMap);
		}
		// 将do转换为vo
		List<DispatchGroupVO> taskVOs = new ArrayList<DispatchGroupVO>();
		if (pageDO.getData() != null) {
			for (DispatchGroupDO taskDO : pageDO.getData()) {
				DispatchGroupVO vo = copyDOToVO(taskDO);
//						vo.setGroupName(getTaskGroupName(vo.getGroupid()));
				taskVOs.add(vo);
			}
		}
		page.setData(taskVOs);
		page.setOrder(pageDO.getOrder());
		page.setPage(pageDO.getPage());
		page.setPageSize(pageDO.getPageSize());
		page.setSort(pageDO.getSort());
		page.setTotal(pageDO.getTotal());
		getDispatchGroupRefList(pageDO.getData());
		return page;
		
	}
	private List<DispatchGroupVO> getDispatchGroupRefList(List<DispatchGroupDO> customerList) {
		List<FieldRelation<DispatchGroupDO>> relations = new ArrayList<FieldRelation<DispatchGroupDO>>();
		//任务分组参照
		relations.add(new RefFieldRelation<DispatchGroupDO>(DispatchGroupDO.class, "id", new String[]{"name"}, baseDocDetailBizService, "selectFieldsByIds"));
		new FieldRelationHandler<DispatchGroupDO>().handler(customerList, relations);
		List<DispatchGroupVO> taskGroupRefVOList = new ArrayList<DispatchGroupVO>();
		for(DispatchGroupDO groupDO : customerList){
			DispatchGroupVO vo = copyDOToVO(groupDO);
			taskGroupRefVOList.add(vo);
		}
		return taskGroupRefVOList;
	}
	//参照列表
	public List<DispatchGroupVO> getRefList(Map<String, Object> paraMap) {
		if (paraMap == null) {
			paraMap = new HashMap<String, Object>();
		}
		paraMap.put("tenantid", InvocationInfoProxy.getTenantid());
		
		List<DispatchGroupDO>  taskGroupRefDOList= dao.selectVoList(DispatchGroupDO.class, "getRefList", paraMap);
		List<DispatchGroupVO> taskGroupRefVOList = new ArrayList<DispatchGroupVO>();
		for(DispatchGroupDO groupDO : taskGroupRefDOList){
			DispatchGroupVO vo = copyDOToVO(groupDO);
			taskGroupRefVOList.add(vo);
		}
		return taskGroupRefVOList;
	}
	/**
	 * 获取参照任务分组列表
	 * @return
	 */
	public Map<String,String> getTaskGroupRefNameMap(){
		Map<String, Object> paraMap = new HashMap<String, Object>();
		paraMap.put("tenantid", InvocationInfoProxy.getTenantid());
		List<DispatchGroupDO>  taskGroupRefDOList= dao.selectVoList(DispatchGroupDO.class, "getRefList", paraMap);
		Map<String,String> groupRefMap = new HashMap<String, String>();
		if(taskGroupRefDOList != null && taskGroupRefDOList.size() >0){
			for(DispatchGroupDO groupDO : taskGroupRefDOList){
				groupRefMap.put(groupDO.getId(), groupDO.getName());
			}
		}
		return groupRefMap;
	}
}
